package com.tianhui.consignortwofei.mvp.ui.activity;

import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fgs.common.CommonFragment;
import com.fgs.common.CommonPresenter;
import com.fgs.common.CommonResponse;
import com.fgs.common.CommonView;
import com.fgs.common.entity.CommonDataListInfo;
import com.fgs.common.progress.ExceptionHandle;
import com.fgs.common.progress.ObserverResponseListener;
import com.fgs.common.utils.DateUtil;
import com.tianhui.consignortwofei.R;
import com.tianhui.consignortwofei.base.BaseDataListActivity;
import com.tianhui.consignortwofei.manager.SharedPreferenceManager;
import com.tianhui.consignortwofei.mvp.model.enty.CapitalData;
import com.tianhui.consignortwofei.mvp.model.enty.CapitalInfo;
import com.tianhui.consignortwofei.mvp.model.UserInfoModel;
import com.tianhui.consignortwofei.mvp.model.enty.AccountDetailInfo;
import com.tianhui.consignortwofei.mvp.ui.adapter.AccountDetailAdapter;
import com.xuexiang.xui.widget.picker.widget.TimePickerView;
import com.xuexiang.xui.widget.picker.widget.builder.TimePickerBuilder;
import com.xuexiang.xui.widget.picker.widget.listener.OnTimeSelectListener;

import org.yczbj.ycrefreshviewlib.adapter.RecyclerArrayAdapter;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author fgs
 * @date 2020/9/22
 * @description
 **/
public class AccountDetailActivity extends BaseDataListActivity {

    @BindView(R.id.activity_account_detail_dateLinearLayout)
    LinearLayout mDateLinearLayout;
    @BindView(R.id.activity_account_detail_dateTextView)
    TextView mDateTextView;
    @BindView(R.id.activity_account_detail_totalAmountTextView)
    TextView mTotalAmountTextView;
    private UserInfoModel mUserInfoModel;
    private TimePickerView mTimePickerView;
    private String mFirstDayDate;
    private String mLastDayDate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_account_detail;
    }

    @Override
    public CommonPresenter createPresenter() {
        return null;
    }

    @Override
    public CommonView createView() {
        return null;
    }

    @Override
    public String getToolbarTitle() {
        return "账户明细";
    }


    @Override
    public RecyclerArrayAdapter createAdapter() {
        return new AccountDetailAdapter(this);
    }

    @Override
    protected void initListener() {

    }

    @Override
    protected int getItemLineHeight() {
        return 1;
    }

    @Override
    public void requestDataList(int page, boolean isDialog) {

        Map<String, String> map = new HashMap<>();
        map.put("page", page + "");
        map.put("limit", "10");
        map.put("id", "166");
        map.put("order", "desc");
        map.put("sort", "create_time");
        if (!TextUtils.isEmpty(mFirstDayDate) && !TextUtils.isEmpty(mLastDayDate)) {
            map.put("startDate", mFirstDayDate);
            map.put("endDate", mLastDayDate);
        }
        mUserInfoModel.accountDetailListModel(this, map, isDialog, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<CommonDataListInfo<AccountDetailInfo>>>() {
            @Override
            public void onNext(CommonResponse<CommonDataListInfo<AccountDetailInfo>> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    CommonDataListInfo commonDataListInfo = commonResponse.getData();
                    setPageInfo(commonDataListInfo.current, commonDataListInfo.pages);
                    List<AccountDetailInfo> list = commonDataListInfo.records;
                    if (list != null && list.size() > 0) {
                        showDataList(list);
                    } else {
                        showEmptyView();
//                        stopRefresh();
                    }

                } else {
                    showToast(commonResponse.getMsg());
                    showEmptyView();
//                    stopRefresh();
                }

            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {
                showToast(e.message);
                showEmptyView();
                stopRefresh();
            }
        });
    }

    @Override
    public void initData() {
        mUserInfoModel = new UserInfoModel();
        String date = DateUtil.getStandardTime(System.currentTimeMillis(), "yyyy 年 MM 月");
        mDateTextView.setText(date);
        mFirstDayDate = DateUtil.getFirstDayOfCurrentMonth();
        mLastDayDate = DateUtil.getLastDayOfCurrentMonth();

        requestCapitalInfo();

    }

    private void requestCapitalInfo() {
        Map<String, String> map = new HashMap<>();
//        map.put("userId", SharedPreferenceManager.getInstance().getConsignorId());
        map.put("customerno", SharedPreferenceManager.getInstance().getRoleId());
        mUserInfoModel.capitalInfoModel(this, map, false, this.bindLifecycle(), new ObserverResponseListener<CommonResponse<CapitalData>>() {

            @Override
            public void onNext(CommonResponse<CapitalData> commonResponse) {
                if (commonResponse.getCode() == 20000) {
                    CapitalData capitalData = commonResponse.getData();
                    if (capitalData != null) {
                        CapitalInfo capitalInfo = capitalData.customerCapital;
                        if (capitalInfo != null) {

                            if (!TextUtils.isEmpty(capitalInfo.expenditure)) {
                                BigDecimal result = new BigDecimal(capitalInfo.expenditure);
                                mTotalAmountTextView.setText(result + "元");
                            } else {
                                mTotalAmountTextView.setText("0元");
                            }
                        }
                    }
                }
            }

            @Override
            public void onError(ExceptionHandle.ResponseThrowable e) {

            }
        });
    }

    @Override
    public CommonFragment createFragment() {
        return null;
    }

    @OnClick(R.id.activity_account_detail_dateLinearLayout)
    public void date() {
        if (mTimePickerView == null) {
            mTimePickerView = new TimePickerBuilder(this, new OnTimeSelectListener() {
                @Override
                public void onTimeSelected(Date date, View v) {
                    String time = DateUtil.getStringByFormat(date, "yyyy 年 MM 月");
                    mDateTextView.setText(time);

                    int year = Integer.parseInt(DateUtil.getStandardTime(date.getTime(), "yyyy"));
                    int month = Integer.parseInt(DateUtil.getStandardTime(date.getTime(), "MM"));

                    mFirstDayDate = DateUtil.getFirstDayOfMonth(year, month);
                    mLastDayDate = DateUtil.getLastDayOfMonth(year, month);

                    refreshData(true);
                }
            })
                    .setTitleText("请选择日期")
                    .setType(true, true, false, false, false, false)
                    .build();
        }
        mTimePickerView.show();
    }
}
